Data

kenya poverty data Uganda poverty data Tanzania poverty data Tanzania poverty data

Packages used

library(plotly)
library(tidyverse)
library(data.table)
library(sf)
library(DT)
library(tmap)
library(ggthemes)

Read data

  • Read data and row bind
poverty_ken <- fread("poverty_data/poverty_ken.csv")

poverty_uga <- fread("poverty_data/poverty_uga.csv")

poverty_tza <- fread("poverty_data/poverty_tza.csv")

poverty_rwa <- fread("poverty_data/poverty_rwa.csv")

poverty_ea <- rbind(poverty_ken, poverty_uga, poverty_rwa, poverty_tza)

poverty_ea[sample(nrow(poverty_ea), 10)] %>% datatable(options = list(scrollX = TRUE))

Minor Cleaning

  • Convert column names into lower
  • Replace space with underscore
nms_old <- names(poverty_ea) 
nms_old
## [1] "Country Name"   "Country ISO3"   "Year"           "Indicator Name"
## [5] "Indicator Code" "Value"
nms_new <- nms_old %>% tolower() 
nms_new <- gsub("\\s", "_", nms_new)
nms_new
## [1] "country_name"   "country_iso3"   "year"           "indicator_name"
## [5] "indicator_code" "value"
setnames(poverty_ea, nms_old, nms_new)
poverty_ea[, value:= as.numeric(value)]
poverty_ea <-  poverty_ea[!is.na(value)]

poverty_ea[sample(nrow(poverty_ea), 10)] %>% datatable(options = list(scrollX = TRUE))

Plots

poverty_ea[, year := as.numeric(year)]

poverty_ea <- poverty_ea[!grepl("^Annu", indicator_name)]
poverty_ea_split <- split(poverty_ea, f = poverty_ea$indicator_name)

n <- length(poverty_ea_split)
my_plots <-htmltools::tagList()
for (i in 1:n) {
    df = poverty_ea_split[[i]]
    my_title = df[, unique(indicator_name)]
    mn = df[, min(year)]
    mx = df[, max(year)]
    breaks = seq(mn, mx,by = 2)
    p = ggplot(df, aes(year, value, group = country_name, color = country_name) ) +
        geom_line()+
        theme_fivethirtyeight()+
        labs(title = my_title, x = "year", y = "%")+
      scale_color_viridis_d(name="")+
      scale_x_continuous(breaks = breaks)
    
    my_plots[[i]] = ggplotly(p)
    
}

my_plots
economy_and_growth <- fread("API_3_DS2_en_csv_v2_1743760.csv") 

nms_old <- economy_and_growth[1,]  %>% as.character()
nms_old
##  [1] "Country Name"   "Country Code"   "Indicator Name" "Indicator Code"
##  [5] "1960"           "1961"           "1962"           "1963"          
##  [9] "1964"           "1965"           "1966"           "1967"          
## [13] "1968"           "1969"           "1970"           "1971"          
## [17] "1972"           "1973"           "1974"           "1975"          
## [21] "1976"           "1977"           "1978"           "1979"          
## [25] "1980"           "1981"           "1982"           "1983"          
## [29] "1984"           "1985"           "1986"           "1987"          
## [33] "1988"           "1989"           "1990"           "1991"          
## [37] "1992"           "1993"           "1994"           "1995"          
## [41] "1996"           "1997"           "1998"           "1999"          
## [45] "2000"           "2001"           "2002"           "2003"          
## [49] "2004"           "2005"           "2006"           "2007"          
## [53] "2008"           "2009"           "2010"           "2011"          
## [57] "2012"           "2013"           "2014"           "2015"          
## [61] "2016"           "2017"           "2018"           "2019"          
## [65] "2020"           "NA"
economy_and_growth <- economy_and_growth[-1,]
names(economy_and_growth) <- nms_old
nms_new <- nms_old %>% tolower() 
nms_new <- gsub("\\s", "_", nms_new)
nms_new
##  [1] "country_name"   "country_code"   "indicator_name" "indicator_code"
##  [5] "1960"           "1961"           "1962"           "1963"          
##  [9] "1964"           "1965"           "1966"           "1967"          
## [13] "1968"           "1969"           "1970"           "1971"          
## [17] "1972"           "1973"           "1974"           "1975"          
## [21] "1976"           "1977"           "1978"           "1979"          
## [25] "1980"           "1981"           "1982"           "1983"          
## [29] "1984"           "1985"           "1986"           "1987"          
## [33] "1988"           "1989"           "1990"           "1991"          
## [37] "1992"           "1993"           "1994"           "1995"          
## [41] "1996"           "1997"           "1998"           "1999"          
## [45] "2000"           "2001"           "2002"           "2003"          
## [49] "2004"           "2005"           "2006"           "2007"          
## [53] "2008"           "2009"           "2010"           "2011"          
## [57] "2012"           "2013"           "2014"           "2015"          
## [61] "2016"           "2017"           "2018"           "2019"          
## [65] "2020"           "na"
setnames(economy_and_growth, nms_old, nms_new)

id_vars <- c("country_name", "country_code",
             "indicator_name", "indicator_code")

economy_and_growth <- melt(economy_and_growth,
                                id.vars = id_vars, 
                           variable.factor = F,
                           value.factor = F,
                           variable.name = "year")
economy_and_growth[, value:= as.numeric(value)]

economy_and_growth[, year := as.numeric(year)]
economy_and_growth <-  economy_and_growth[!is.na(value)|!is.na(year)]
economy_and_growth[sample(nrow(economy_and_growth), 10)] %>% datatable(options = list(scrollX = TRUE))
gni_gdp_savings_vec <- c("Gross savings (% of GNI)", 
                         "Gross savings (% of GDP)",
                         "Total debt service (% of exports of goods, services and primary income)",
                         "Total debt service (% of GNI)",
                         "Trade (% of GDP)",
                         "Current account balance (% of GDP)",
                         "Exports of goods and services (annual % growth)",
                         "Manufacturing, value added (annual % growth)",
                         "Price level ratio of PPP conversion factor (GDP) to market exchange rate")
ea_country <- c("Kenya", "Uganda", "Korea, Rep.", "Tanzania", "Singapore")
economy_and_growth[, unique(country_name)]
##   [1] "Aruba"                                               
##   [2] "Afghanistan"                                         
##   [3] "Angola"                                              
##   [4] "Albania"                                             
##   [5] "Andorra"                                             
##   [6] "Arab World"                                          
##   [7] "United Arab Emirates"                                
##   [8] "Argentina"                                           
##   [9] "Armenia"                                             
##  [10] "American Samoa"                                      
##  [11] "Antigua and Barbuda"                                 
##  [12] "Australia"                                           
##  [13] "Austria"                                             
##  [14] "Azerbaijan"                                          
##  [15] "Burundi"                                             
##  [16] "Belgium"                                             
##  [17] "Benin"                                               
##  [18] "Burkina Faso"                                        
##  [19] "Bangladesh"                                          
##  [20] "Bulgaria"                                            
##  [21] "Bahrain"                                             
##  [22] "Bahamas, The"                                        
##  [23] "Bosnia and Herzegovina"                              
##  [24] "Belarus"                                             
##  [25] "Belize"                                              
##  [26] "Bermuda"                                             
##  [27] "Bolivia"                                             
##  [28] "Brazil"                                              
##  [29] "Barbados"                                            
##  [30] "Brunei Darussalam"                                   
##  [31] "Bhutan"                                              
##  [32] "Botswana"                                            
##  [33] "Central African Republic"                            
##  [34] "Canada"                                              
##  [35] "Central Europe and the Baltics"                      
##  [36] "Switzerland"                                         
##  [37] "Channel Islands"                                     
##  [38] "Chile"                                               
##  [39] "China"                                               
##  [40] "Cote d'Ivoire"                                       
##  [41] "Cameroon"                                            
##  [42] "Congo, Dem. Rep."                                    
##  [43] "Congo, Rep."                                         
##  [44] "Colombia"                                            
##  [45] "Comoros"                                             
##  [46] "Cabo Verde"                                          
##  [47] "Costa Rica"                                          
##  [48] "Caribbean small states"                              
##  [49] "Cuba"                                                
##  [50] "Curacao"                                             
##  [51] "Cayman Islands"                                      
##  [52] "Cyprus"                                              
##  [53] "Czech Republic"                                      
##  [54] "Germany"                                             
##  [55] "Djibouti"                                            
##  [56] "Dominica"                                            
##  [57] "Denmark"                                             
##  [58] "Dominican Republic"                                  
##  [59] "Algeria"                                             
##  [60] "East Asia & Pacific (excluding high income)"         
##  [61] "Early-demographic dividend"                          
##  [62] "East Asia & Pacific"                                 
##  [63] "Europe & Central Asia (excluding high income)"       
##  [64] "Europe & Central Asia"                               
##  [65] "Ecuador"                                             
##  [66] "Egypt, Arab Rep."                                    
##  [67] "Euro area"                                           
##  [68] "Eritrea"                                             
##  [69] "Spain"                                               
##  [70] "Estonia"                                             
##  [71] "Ethiopia"                                            
##  [72] "European Union"                                      
##  [73] "Fragile and conflict affected situations"            
##  [74] "Finland"                                             
##  [75] "Fiji"                                                
##  [76] "France"                                              
##  [77] "Faroe Islands"                                       
##  [78] "Micronesia, Fed. Sts."                               
##  [79] "Gabon"                                               
##  [80] "United Kingdom"                                      
##  [81] "Georgia"                                             
##  [82] "Ghana"                                               
##  [83] "Gibraltar"                                           
##  [84] "Guinea"                                              
##  [85] "Gambia, The"                                         
##  [86] "Guinea-Bissau"                                       
##  [87] "Equatorial Guinea"                                   
##  [88] "Greece"                                              
##  [89] "Grenada"                                             
##  [90] "Greenland"                                           
##  [91] "Guatemala"                                           
##  [92] "Guam"                                                
##  [93] "Guyana"                                              
##  [94] "High income"                                         
##  [95] "Hong Kong SAR, China"                                
##  [96] "Honduras"                                            
##  [97] "Heavily indebted poor countries (HIPC)"              
##  [98] "Croatia"                                             
##  [99] "Haiti"                                               
## [100] "Hungary"                                             
## [101] "IBRD only"                                           
## [102] "IDA & IBRD total"                                    
## [103] "IDA total"                                           
## [104] "IDA blend"                                           
## [105] "Indonesia"                                           
## [106] "IDA only"                                            
## [107] "Isle of Man"                                         
## [108] "India"                                               
## [109] "Not classified"                                      
## [110] "Ireland"                                             
## [111] "Iran, Islamic Rep."                                  
## [112] "Iraq"                                                
## [113] "Iceland"                                             
## [114] "Israel"                                              
## [115] "Italy"                                               
## [116] "Jamaica"                                             
## [117] "Jordan"                                              
## [118] "Japan"                                               
## [119] "Kazakhstan"                                          
## [120] "Kenya"                                               
## [121] "Kyrgyz Republic"                                     
## [122] "Cambodia"                                            
## [123] "Kiribati"                                            
## [124] "St. Kitts and Nevis"                                 
## [125] "Korea, Rep."                                         
## [126] "Kuwait"                                              
## [127] "Latin America & Caribbean (excluding high income)"   
## [128] "Lao PDR"                                             
## [129] "Lebanon"                                             
## [130] "Liberia"                                             
## [131] "Libya"                                               
## [132] "St. Lucia"                                           
## [133] "Latin America & Caribbean"                           
## [134] "Least developed countries: UN classification"        
## [135] "Low income"                                          
## [136] "Liechtenstein"                                       
## [137] "Sri Lanka"                                           
## [138] "Lower middle income"                                 
## [139] "Low & middle income"                                 
## [140] "Lesotho"                                             
## [141] "Late-demographic dividend"                           
## [142] "Lithuania"                                           
## [143] "Luxembourg"                                          
## [144] "Latvia"                                              
## [145] "Macao SAR, China"                                    
## [146] "St. Martin (French part)"                            
## [147] "Morocco"                                             
## [148] "Monaco"                                              
## [149] "Moldova"                                             
## [150] "Madagascar"                                          
## [151] "Maldives"                                            
## [152] "Middle East & North Africa"                          
## [153] "Mexico"                                              
## [154] "Marshall Islands"                                    
## [155] "Middle income"                                       
## [156] "North Macedonia"                                     
## [157] "Mali"                                                
## [158] "Malta"                                               
## [159] "Myanmar"                                             
## [160] "Middle East & North Africa (excluding high income)"  
## [161] "Montenegro"                                          
## [162] "Mongolia"                                            
## [163] "Northern Mariana Islands"                            
## [164] "Mozambique"                                          
## [165] "Mauritania"                                          
## [166] "Mauritius"                                           
## [167] "Malawi"                                              
## [168] "Malaysia"                                            
## [169] "North America"                                       
## [170] "Namibia"                                             
## [171] "New Caledonia"                                       
## [172] "Niger"                                               
## [173] "Nigeria"                                             
## [174] "Nicaragua"                                           
## [175] "Netherlands"                                         
## [176] "Norway"                                              
## [177] "Nepal"                                               
## [178] "Nauru"                                               
## [179] "New Zealand"                                         
## [180] "OECD members"                                        
## [181] "Oman"                                                
## [182] "Other small states"                                  
## [183] "Pakistan"                                            
## [184] "Panama"                                              
## [185] "Peru"                                                
## [186] "Philippines"                                         
## [187] "Palau"                                               
## [188] "Papua New Guinea"                                    
## [189] "Poland"                                              
## [190] "Pre-demographic dividend"                            
## [191] "Puerto Rico"                                         
## [192] "Korea, Dem. Peopleâ\200\231s Rep."                         
## [193] "Portugal"                                            
## [194] "Paraguay"                                            
## [195] "West Bank and Gaza"                                  
## [196] "Pacific island small states"                         
## [197] "Post-demographic dividend"                           
## [198] "French Polynesia"                                    
## [199] "Qatar"                                               
## [200] "Romania"                                             
## [201] "Russian Federation"                                  
## [202] "Rwanda"                                              
## [203] "South Asia"                                          
## [204] "Saudi Arabia"                                        
## [205] "Sudan"                                               
## [206] "Senegal"                                             
## [207] "Singapore"                                           
## [208] "Solomon Islands"                                     
## [209] "Sierra Leone"                                        
## [210] "El Salvador"                                         
## [211] "San Marino"                                          
## [212] "Somalia"                                             
## [213] "Serbia"                                              
## [214] "Sub-Saharan Africa (excluding high income)"          
## [215] "South Sudan"                                         
## [216] "Sub-Saharan Africa"                                  
## [217] "Small states"                                        
## [218] "Sao Tome and Principe"                               
## [219] "Suriname"                                            
## [220] "Slovak Republic"                                     
## [221] "Slovenia"                                            
## [222] "Sweden"                                              
## [223] "Eswatini"                                            
## [224] "Sint Maarten (Dutch part)"                           
## [225] "Seychelles"                                          
## [226] "Syrian Arab Republic"                                
## [227] "Turks and Caicos Islands"                            
## [228] "Chad"                                                
## [229] "East Asia & Pacific (IDA & IBRD countries)"          
## [230] "Europe & Central Asia (IDA & IBRD countries)"        
## [231] "Togo"                                                
## [232] "Thailand"                                            
## [233] "Tajikistan"                                          
## [234] "Turkmenistan"                                        
## [235] "Latin America & the Caribbean (IDA & IBRD countries)"
## [236] "Timor-Leste"                                         
## [237] "Middle East & North Africa (IDA & IBRD countries)"   
## [238] "Tonga"                                               
## [239] "South Asia (IDA & IBRD)"                             
## [240] "Sub-Saharan Africa (IDA & IBRD countries)"           
## [241] "Trinidad and Tobago"                                 
## [242] "Tunisia"                                             
## [243] "Turkey"                                              
## [244] "Tuvalu"                                              
## [245] "Tanzania"                                            
## [246] "Uganda"                                              
## [247] "Ukraine"                                             
## [248] "Upper middle income"                                 
## [249] "Uruguay"                                             
## [250] "United States"                                       
## [251] "Uzbekistan"                                          
## [252] "St. Vincent and the Grenadines"                      
## [253] "Venezuela, RB"                                       
## [254] "British Virgin Islands"                              
## [255] "Virgin Islands (U.S.)"                               
## [256] "Vietnam"                                             
## [257] "Vanuatu"                                             
## [258] "World"                                               
## [259] "Samoa"                                               
## [260] "Kosovo"                                              
## [261] "Yemen, Rep."                                         
## [262] "South Africa"                                        
## [263] "Zambia"                                              
## [264] "Zimbabwe"
gni_gdp_savings_df <- economy_and_growth[indicator_name %in% gni_gdp_savings_vec & country_name %in% ea_country ]
gni_gdp_savings_df_split <- split(gni_gdp_savings_df, f = gni_gdp_savings_df$indicator_name)
n <- length(gni_gdp_savings_df_split)
my_plots_econ <-htmltools::tagList()
for (i in 1:n) {
    df = gni_gdp_savings_df_split[[i]]
    my_title = df[, unique(indicator_name)]
    mn = df[, min(year)]
    mx = df[, max(year)]
    breaks = seq(mn, mx,by = 5)
    p = ggplot(df, aes(year, value, group = country_name, color = country_name) ) +
        geom_line(size = .3)+
        theme_hc()+
        labs(title = my_title, x = "year", y = "%")+
      scale_color_colorblind(name="")+
      scale_x_continuous(breaks = breaks)
    
    my_plots_econ[[i]] = ggplotly(p)
    
}

my_plots_econ